/*
 Component SmileyTextArea,
   which is intended to be part of Eteria IRC Client from Javier Kohen
 2001 written by Frank Bartels for Splendid Internet GmbH, Kiel, Germany
 */

 package com.splendid.awtchat;

 import java.io.*;
 import java.awt.*;
 import java.awt.image.ImageObserver;

 import java.awt.event.*;
 import java.util.*;

 import ar.com.jkohen.awt.NickInfoPopup;
 import ar.com.jkohen.awt.event.NewMouseWheelEvent;
 import ar.com.jkohen.util.Resources;



 public class SmileyTextArea extends Panel implements ImageObserver
 {
   public static Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN, 12);

   private static Resources res;

   public static int SMOOTH = 0;
   public static int FAST = 1;
   public static int SAFE = 2;  // mode's possible values


   private SmileyTextAreaArea smileyText; // the text area

   private Scrollbar scrollbar;		   // the scrollbar

   private NickInfoPopup nick_pop;

   // stuff for saving the smileys (prepared on init())

   static Hashtable smileys = null;
   static int maxCodeLength = 1;
   static int maxSmileyHeight = 0;
   static boolean staticinitialized = false;

   // ====== Constructor, init() ======


   /** Constructor. supply HyperlinkReceiver for getting the clicked URLs
 	*/

 	public SmileyTextArea(HyperlinkReceiver hr, CopyText cp)
 	{
 		super();
 		setLayout(new BorderLayout());
 		nick_pop = new NickInfoPopup();
 		scrollbar = new Scrollbar(Scrollbar.VERTICAL, 5, 5, 0, 10); //val,vis,min,max 0,5,0,10->0..5

 		//scrollbar.setBackground(SystemColor.text);

 		smileyText = new SmileyTextAreaArea(this, scrollbar, hr, cp, nick_pop);
 		add(smileyText, "Center");
 		add(scrollbar, "East");
 		add(nick_pop, "West");
 		init();

 		enableWheelEvt(true);
 		enableEvents(WindowEvent.WINDOW_EVENT_MASK);

 		// Determine the maximum height of smileys

 		if (smileys != null)
 		{
 			for (Enumeration e = smileys.elements(); e.hasMoreElements(); )
 			{

 				Image i = (Image)e.nextElement();
 				if (i != null)
 				{
 					int h = i.getHeight(this);
 					if (maxSmileyHeight < h)
 					maxSmileyHeight = h;
 				}
 			}
 		}
 	}

   // Is called above

 	public static void init()
 	{
 //		this.sp = sp;

 		if (staticinitialized)
 			return;

 		smileys = new Hashtable();

 //		Read smileys from resource file

 //		EIRC WORKAROUND #35.

 //	  Resource res=Resource.getBundle("com.splendid.awtchat.Smileys");


 		maxCodeLength = 1;
 		for (Enumeration e = res.img_resource.propertyNames(); e.hasMoreElements() ;)
 		{
 			String key = (String)e.nextElement();

 //			this version needs entries like (you could use 123 or thisnthat instead of sad):

 //			smiley.sad.emoticons=:-( :( :-<

 //			smiley.sad.image=smileys/sm_sad.gif


 			int imagepos = key.indexOf(".image");
 			if ((key.indexOf("smiley.") == 0 || key.indexOf("symbol.") == 0) && imagepos >= 8)
 			{
 				try
 				{
 					String filename = res.getLabel(key);
 					String smiley = res.getLabel(key.substring(0, imagepos) + ".emoticons");
 					StringTokenizer st = new StringTokenizer(smiley);
 					while (st.hasMoreTokens())
 					{
 						String code = (String)st.nextToken();	// the other are the ascii codes

 						Image image = res.getImage(key);
 						smileys.put(code, image);
 						if (code.length() > maxCodeLength)
 							maxCodeLength = code.length();
 					}
 				}
 				catch (MissingResourceException mre) {}			// ignore errors

 		  	}

 //		this version commented out: smiley.sad=smileys/sm_sad.gif :-( :( :-<

 //			if (key.indexOf("smiley.") == 0)

 //			{

 //				String smiley = res.getString(key);

 //				StringTokenizer st = new StringTokenizer(smiley);

 //				String filename = (String)st.nextToken();		// first token is filename

 //				while (st.hasMoreTokens())

 //				{

 //					String code = (String)st.nextToken();		// the other are the ascii codes

 //					addSmiley(filename, code, sp);

 //					if (code.length()>maxCodeLength)

 //						maxCodeLength = code.length();

 //				}

 //			}

 		}
 		staticinitialized = true;
 	}

 	public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
 	{
 		return(true);
 	}

 //	public synchronized void addTextListener(TextListener l)

 //	{

 //	}


   // ====== SubsequentIndent ======


   /** set the indent of subsequent lines (>=0) if a line must be split (default is 12)
 	*/

   public void setSubsequentIndent(int indent) // allowed values: 0 and greater

   { if (indent<0) indent=0;
 	smileyText.setSubsequentIndent(indent);
   }

   /** get the indent of subsequent lines if a line must be split (default is 12)
 	*/

   public int getSubsequentIndent()
   { return(smileyText.getSubsequentIndent());
   }

   // ====== Colors ======


   public void setBackground(Color c)
   {
   	smileyText.setBackground(c);
   }

   public Color getBackground()
   {
   	return(smileyText.getBackground());
   }

   public void setForeground(Color c)
   {
   	smileyText.setForeground(c);
   }

   public Color getForeground()
   {
   	return(smileyText.getForeground());
   }

   public void setSelectedBackground(Color c)
   {
   	smileyText.setSelectedBackground(c);
   }

   public Color getSelectedBackground()
   {
   	return(smileyText.getSelectedBackground());
   }

   /** change one of the color paette entries (n may be a number from 0 to 15,
 	* it corresponds to the number associated with MircMessage.COLOR IRC attribute)
 	*/
   public void setColorPaletteEntry(int n, Color c)
   {
   	smileyText.setColorPaletteEntry(n, c);
   }

   /** get one of the color palette entries (n may be a number from 0 to 15,
 	* it corresponds to the number associated with MircMessage.COLOR IRC attribute)
 	*/

   public Color getColorPaletteEntry(int n)
   {
   	return(smileyText.getColorPaletteEntry(n));
   }

   /** get the static color palette.
 	*/

   public static Color [] getColorPalette()
   {
   	return(SmileyTextAreaArea.fixedColors);
   }

   public static void setColors(Color palette[])
   {
 	SmileyTextAreaArea.setColorPalette(palette);
   }

   // ====== Font ======


   /** change the font (only name and size are used)
 	*/

   public void setFont(Font f)
   {
   	smileyText.changeFont(f.getName(), f.getSize());
   }

   /** change the font
 	*/

   public void setFont(String name, int size)
   {
   	smileyText.changeFont(name, size);
   }

   /** get the actual base font (not bold and not italic)
 	*/

   public Font getFont()
   {
   	return(smileyText.getFont());
   }

   // ====== Append ======


   /** set the length of the text buffer (number of appends after which old text is deleted)
 	  (set -1 for infinite buffer)
 	*/

   public void setBufferlen(int bufferlen)
   {
   	smileyText.setBufferlen(bufferlen);
   }

   /** get the length of the text buffer (number of appends after which old text is deleted)
 	  (-1 means infinite buffer)
 	*/

   public int getBufferlen()
   {
   	return(smileyText.getBufferlen());
   }

   // ====== Append ======


   /** insert a line of text (smileys, URLs and IRC attributes interpreted)
 	* (URLs == http://* | https://* | ftp://* | www.* | #*  (case is ignored))
 	* (IRC attributes are (from MircMessage) BELL, BOLD, ITALIC,
 	*  UNDERLINE, COLOR, REVERSE, RESET)
 	*/

   public void append(String s)
   {
   	if (s == null || s.length() == 0)
 		s = "\n";
  	smileyText.append(s, true);
   }

   /** same as append, except, URL interpretation is suppressed (channels, email, web-adresses)
 	*/

   public void appendNoUrls(String s)
   {
   	smileyText.append(s, false);
   }

   /** same as append, but add a NickInfoPopup
 	*/

   public void appendNickInfo(String s, boolean b, String n)
   {
  	smileyText.append(s, b, n);
   }
 	// ====== Scroll ======


   public void home()
   {
   	smileyText.setSbTop();
   }

   /** Scrolling display mode, may be SMOOTH, FAST, SAFE (initially it is SMOOTH)
 	* Set mode to FAST if problems arise with smooth scrolling
 	* Set mode to SAFE if problems arise with any scrolling (always pains the whole)
 	*/

   public void setMode(int mode)
   {
   	smileyText.setMode(mode);
   }

   //** Define if each line can be broken into multiples lines

   public void setBreaks(boolean canBreak)
   {
   	smileyText.setBreaks(canBreak);
   }

   //** Define tabulations

   public void setTabs(int tabs[])
   {
   	for (int i = 0; i < tabs.length; i++)
   		smileyText.addTab(tabs[i]);
   }


   //** Erase everything

   public void clear()
   {
   	smileyText.clean();
   }

   public void setSelectable(boolean b)
   {
   	smileyText.isSelectable = b;
   }

   /* Mouse Wheel support */

 	public void enableWheelEvt(boolean b)
 	{
 		if (b && NewMouseWheelEvent.EventMask > 0)
 			enableEvents(NewMouseWheelEvent.EventMask);
 		else
 			disableEvents(NewMouseWheelEvent.EventMask);
 	}

 	public void mouseWheelMoved(NewMouseWheelEvent ev)
 	{
 		if (ev != null)
 			smileyText.setRawSbValue(scrollbar.getValue() + ev.getUnitsToScroll());
 	}

 	public void processEvent(AWTEvent ev)
 	{
 		if (ev.getClass() == NewMouseWheelEvent.EventClass)
 			mouseWheelMoved(new NewMouseWheelEvent((MouseEvent)ev));

 		super.processEvent(ev);
 	}
 }